/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import edu.cmu.lti.dimple.mavuno.JudgedParaphrases;
import edu.cmu.lti.dimple.mavuno.LearnedParaphrases;
import edu.cmu.lti.dimple.mavuno.PerTermParaphraser;
import edu.cmu.lti.dimple.nlp.SnowballStemmer;

/**
 * Implements the Expected Precision with -R (redundancy) algorithm.
 * 
 * @author Hideki Shima
 *
 */
public class ExpectedPrecisionAlgorithm {

  //rankingMode = true ? AP : Expected Prec (as in paper)
  public List<Double> calcEP( List<LearnedParaphrases> lps, JudgedParaphrases jp, 
          String targetTerm, int listSize, boolean rankingMode, boolean ignoreRedundant ) {
    List<Double> result = new ArrayList<Double>();
    
    StringBuilder sb = new StringBuilder();
    for ( LearnedParaphrases lp : lps ) {
      PerTermParaphraser ptp = lp.get(targetTerm);
      if (ptp==null){
        System.out.println("No paraphrase available: "+lp.getAlgorighm()+" for "+targetTerm);
        continue;
      }
      
      List<String> paraphrases = ptp.getParaphrases();
//      if (paraphrases.size()!=10) System.out.println(targetTerm+"\t"+resultFile+"\t"+paraphrases.size()); 
      sb.append("---- "+targetTerm+"\t"+lp.getAlgorighm()+" ----\n");
      double psum = 0;
      double sum = 0;
      int min = Math.min(listSize, paraphrases.size());
      for (int i=0; i<min; i++) {
        String paraphrase = paraphrases.get(i);
        double w = jp.getMean(targetTerm, paraphrase);
        if (ignoreRedundant) {
          String targetRoot = SnowballStemmer.stem(targetTerm);
          String[] paraphraseWords = paraphrase.split("[^a-zA-Z]+");
          for ( String word : paraphraseWords ) {
            String r = SnowballStemmer.stem(word);
            if ( r.equals(targetRoot) ) {
              w = 0;
              break;
            }
          }
        }
        sb.append(w+"\t"+targetTerm +"\t"+ paraphrase+"\n");
        psum += w;
        sum += psum/(double)(i+1);
      }
      // if paraphrases smaller than list
      for (int i=paraphrases.size(); i<listSize ;i++) {
        sum += psum/(double)(i+1);
      }
      double ap = rankingMode ? (sum/(double)listSize) : (psum/(double)listSize);
      sb.append( (rankingMode?"AP":"EP")+" = "+ap+"\n\n" );
      result.add(ap);
    }
    FileUtils.writeFile(sb.toString(), new File("target/logs/AP_"+targetTerm+"_eval.txt"));
    return result;
  }
  
}
